<template>
	<view style="padding: 20rpx;">
		<view class="box">
			<uni-forms :modelValue="form" ref="formRef" :rules="rules" label-width="160rpx" label-align="right">
				<uni-forms-item name="address" label="地址信息" required>
					<uni-easyinput type="textarea" v-model="form.address" placeholder="请输入地址" />
				</uni-forms-item>
				<uni-forms-item name="user" label="联系人" required>
					<uni-easyinput type="text" v-model="form.user" placeholder="请输入联系人" />
				</uni-forms-item>
				<uni-forms-item name="phone" label="联系方式" required>
					<uni-easyinput type="text" v-model="form.phone" placeholder="请输入联系方式" />
				</uni-forms-item>
				<uni-forms-item>
					<button type="primary" size="mini" @click="saveAddress">确定</button>
				</uni-forms-item>
			</uni-forms>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				rules: {
					address: {
						rules: [{
							required: true,
							errorMessage: '请填写地址',
						}, ]
					},
					user: {
						rules: [{
							required: true,
							errorMessage: '请填写联系人',
						}, ]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '请填写联系方式',
						}, ]
					}
				},
				form: {},
				user: uni.getStorageSync('xm-user'),
			}
		},
		onLoad(option) {
			let addressId = option.addressId||0
			if(addressId>0){
				this.$request.get('/address/selectById/' + addressId).then(res => {
					this.form = res.data || {}
				})
			}
		},
		methods: {
			saveAddress() {
				// 验证
				this.$refs.formRef.validate().then(res => {
					this.$request.request({
						method:this.form.id?'PUT':'POST',
						url:this.form.id?'/address/update':'/address/add',
						data:this.form
					}).then(res => {
						if (res.code === '200') {
							uni.showToast({
								title: '操作成功'
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else {
							uni.showToast({
								icon: 'error',
								title: res.msg
							})
						}
					})
				}).catch(err => {
					console.log('err', err);
				})
			}
		}
	}
</script>

<style>

</style>